package com.fire.service;

import com.fire.common.domain.BusinessException;
import com.fire.common.domain.CommonErrorCode;
import com.fire.common.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author fire
 * @date 2021年07月17日11:34
 */
@Service
public class FileServiceImpl implements FileService{
    @Value("${oss.qiniu.url}")
    private String url;
    @Value("${oss.qiniu.accessKey}")
    private String accessKey ;
    @Value("${oss.qiniu.secretKey}")
    private String secretKey ;
    @Value("${oss.qiniu.bucket}")
    private String bucket ;
    @Override
    public String Upload(byte[] bytes, String filePath) throws BusinessException {
//        调用common下面的工具类
        try {
            QiniuUtil.upload2Qiniuyun(accessKey,secretKey,bucket,bytes,filePath);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new BusinessException(CommonErrorCode.E_100106);
        }
//        如果上传成功，返回文件访问地址（绝对路径）
        return url +"/" +filePath;
    }
}
